package com.hxd.buybooksystem.adapter

import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import cn.bmob.v3.exception.BmobException
import cn.bmob.v3.listener.UpdateListener
import com.hjq.image.ImageLoader
import com.hxd.buybooksystem.R
import com.hxd.buybooksystem.bean.BookInfo
import com.hxd.buybooksystem.bean.Order

/**
 *  项目名：    BuyBookSystem
 *  包名：      com.hxd.buybooksystem.adapter
 *  文件名:     BuyCarOrderAdapter
 *  创建者:     HXD
 *  创建时间:   2019/6/2 15:46
 *  描述:       购物车数据适配
 */

class BuyCarOrderAdapter(private val datas:ArrayList<Order>, private val bookInfo:ArrayList<BookInfo>) : RecyclerView.Adapter<BuyCarOrderAdapter.ViewHolder>(){

//    private var mOnItemClickLitener: OnItemBtnClickLitener? = null
    private var priceSum = arrayListOf(0f, 0f, 0f, 0f, 0f, 0f, 0f)

//    //设置回调接口
//    interface OnItemBtnClickLitener {
//        fun addClick(BuyCount:Int, SurplusCount:Int, countTextView:TextView)
//        fun subClick(BuyCount:Int, SurplusCount:Int, countTextView:TextView)
//    }
//
//    fun setOnItemClickLitener(mOnItemClickLitener: OnItemBtnClickLitener) {
//        this.mOnItemClickLitener = mOnItemClickLitener
//    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_order, parent, false)
        return ViewHolder(view)
    }

    override fun getItemCount(): Int {
        return datas.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val orderItem = datas[position]
        if(!orderItem.isPay){
            ImageLoader.loadImage(holder.image, orderItem.image)
            holder.name.text = orderItem.name
            holder.price.text = orderItem.price
            priceSum[position] = orderItem.price.toFloat()
            holder.surplusCount.text = orderItem.surplusCount.toString()
            holder.buyCount.text = orderItem.buyCount.toString()

            holder.subBtn.setOnClickListener {
                //图书的购买数量大于1时才能减少
                if(orderItem.buyCount > 1){
                    orderItem.buyCount -= 1
                    priceSum[position] -= orderItem.price.toFloat()
                    holder.buyCount.text = orderItem.buyCount.toString()
                }
            }

            holder.addBtn.setOnClickListener {
                //图书的购买数量小于剩余数量时才能加
                if(orderItem.buyCount < orderItem.surplusCount){
                    orderItem.buyCount += 1
                    priceSum[position] += orderItem.price.toFloat()
                    holder.buyCount.text = orderItem.buyCount.toString()
                }
            }
        }
    }

    /**
     * RecyclerView的持有者类
     */
    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var image: ImageView = view.findViewById(R.id.iv_image_order)
        var name: TextView = view.findViewById(R.id.tv_name_order)
        var price: TextView = view.findViewById(R.id.tv_price_order)
        var surplusCount: TextView = view.findViewById(R.id.tv_surplusCount_order)
        var buyCount: TextView = view.findViewById(R.id.tv_buyCount_order)
        val subBtn:ImageView = view.findViewById(R.id.iv_subBtn_order)
        val addBtn:ImageView = view.findViewById(R.id.iv_addBtn_order)
    }

    /**
     * 返回书的价格
     */
    fun getSumPrice(): ArrayList<Float> {
        return priceSum
    }

    /**
     * 设置已经购买了
     */
    fun setPay(){
        datas.forEachIndexed { index, order ->
            order.isPay = true
            order.update(order.objectId, object : UpdateListener(){
                override fun done(e: BmobException?) {
                    if(e == null){
                        bookInfo.forEach {
                            if(it.objectId == order.bookObjectId){
                                it.count = order.surplusCount - order.buyCount
                                it.update(it.objectId, object : UpdateListener(){
                                    override fun done(e: BmobException?) {

                                    }
                                })
                            }
                        }
                    }else{

                    }
                }
            })
        }
    }

    /**
     * 清空数据
     */
    fun setClearData(){
        datas.clear()
    }

}